package com.asj.vivo.lock.service.recover.correct;

import static com.asj.enums.DeviceIdentifierDictEnums.json_key_deviceCode;
import static com.asj.enums.DeviceIdentifierDictEnums.json_key_result;
import static com.asj.enums.HttpRequestDictEnums.exchangeDeviceAppInfoUrl;
import static com.asj.enums.ResultCodeEnums.request_error;
import static com.asj.enums.ResultCodeEnums.result_success;

import android.content.Context;
import android.util.Log;

import com.asj.MainActivity;
import com.asj.service.binding.AppInfoHandlerService;
import com.asj.service.oppoSdk.OPPOSDKService;
import com.asj.utils.CommonsUtils;
import com.asj.utils.HttpRequestUtils;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
 * 矫正设备app信息
 * */
public class CorrectDeviceAppInfo {
    /**
     * 未加入启动名单导致的应用无法启动
     * */
    public static void notInStartupList(MainActivity mainActivity){
        OPPOSDKService opposdkService = new OPPOSDKService();
        //获取在启动名单中的所有应用
        List<String> allowedRunningApp = opposdkService.getAllowedRunningApp();
        //获取所有本地的app
        List<String> deviceAppList = AppInfoHandlerService.getInstance(mainActivity).getDeviceAppList(1);
        //需要补充加入名单的app
        List<String> needReplenishAppList = new ArrayList<>();

        //筛选在名单内部的，仅留下不在允许启动名单内部的
        deviceAppList.forEach(app->{
            if(allowedRunningApp.contains(app)){
                needReplenishAppList.add(app);
            }
        });
        Log.w("未加入启动名单的检测","检测完成，本次补充加入"+needReplenishAppList.size()+"个app");
    }

    /**
     * 监听安装失败，导致应用无法启动，同时后台数据也未同步
     * */
    public static void listenAppInstallLose(Context context){
        OPPOSDKService opposdkService = new OPPOSDKService();
        //app信息处理服务
        AppInfoHandlerService appInfoHandlerService = AppInfoHandlerService.getInstance(context);
        //获取所有本地的app
        List<String> appPackageNameList = appInfoHandlerService.getDeviceAppList(1);
        JsonObject param = new JsonObject();
        param.addProperty(json_key_deviceCode.getValue(),CommonsUtils.getDeviceCode(opposdkService.getIMEI()));
        //请求获取后端中该设备已绑定的应用
        String result = HttpRequestUtils.retryHttpRequest(exchangeDeviceAppInfoUrl.getValue(), param.toString(), result_success.getCode(), 5, context);
        if(result.equals(request_error.getMessage())){
            return;
        }
        //取出后端返回的数据
        JsonObject deviceAppInfo = JsonParser.parseString(result).getAsJsonObject();
        String deviceAppPackageName = deviceAppInfo.get(json_key_result.getValue()).getAsString();
        if(deviceAppPackageName != null && !deviceAppPackageName.isEmpty()){
            //需要传输给后端的数据
            List<String> loseAppInfo = new ArrayList<>();

            List<String> deviceAppPackageNameList = Arrays.asList(deviceAppPackageName.split(","));
            //将后端传输过来的数据与当前设备拥有的数据进行比对，如果存在不一致的情况，则将其传给后端，让其与该设备绑定
            appPackageNameList.forEach(localApp->{
                if(!deviceAppPackageNameList.contains(localApp)){
                    loseAppInfo.add(localApp);
                }
            });
            //收集完毕，进行发送给后端
            if(!loseAppInfo.isEmpty()){
                loseAppInfo.forEach(app->{
                    appInfoHandlerService.transientAppInfo(app,1);
                });
                Log.i("设备应用信息矫正","存在矫正! "+loseAppInfo);
            }else{
                Log.i("设备应用信息矫正","无需矫正");
            }
        }

    }
}
